package com.gobang.domain.message;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * WebSocket消息基础类
 * 所有WebSocket消息的统一基类，包含消息类型和数据
 * 
 * @author System
 * @since 1.0.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
// 在JSON 中添加类型标识符，以便在反序列化时识别具体子类
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
// 列出所有可能的子类以及其对应的类型标识符，建立映射关系
@JsonSubTypes({
    @JsonSubTypes.Type(value = HeartbeatMessage.class, name = "HEARTBEAT"),
    @JsonSubTypes.Type(value = HeartbeatResponseMessage.class, name = "HEARTBEAT_RESPONSE"),
    @JsonSubTypes.Type(value = MoveRequestMessage.class, name = "MAKE_MOVE"),
    @JsonSubTypes.Type(value = MoveResponseMessage.class, name = "MOVE_RESPONSE"),
    @JsonSubTypes.Type(value = GameStateUpdateMessage.class, name = "GAME_STATE_UPDATE"),
    @JsonSubTypes.Type(value = GameStateRequestMessage.class, name = "REQUEST_GAME_STATE"),
    @JsonSubTypes.Type(value = MatchStatusUpdateMessage.class, name = "MATCH_STATUS_UPDATE"),
    @JsonSubTypes.Type(value = MatchSuccessMessage.class, name = "MATCH_SUCCESS"),
    @JsonSubTypes.Type(value = GameOverMessage.class, name = "GAME_OVER"),
    @JsonSubTypes.Type(value = OpponentOfflineMessage.class, name = "OPPONENT_OFFLINE"),
    @JsonSubTypes.Type(value = ErrorResponseMessage.class, name = "ERROR_RESPONSE")
})
public abstract class WebSocketMessage implements Serializable {
    
    /**
     * 消息类型
     */
    private String type;
    
    /**
     * 消息时间戳
     */
    private Long timestamp;
    
    /**
     * 构造函数
     *
     * @param type 消息类型
     */
    public WebSocketMessage(String type) {
        this.type = type;
        this.timestamp = System.currentTimeMillis();
    }
} 